package com.transport.analysis.traffic;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.Text;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Objects;

public class StopHourMinuteWritable implements WritableComparable<StopHourMinuteWritable> {

    private Text stopId;
    private Text hourMinute;

    public StopHourMinuteWritable() {
        this.stopId = new Text();
        this.hourMinute = new Text();
    }

    public StopHourMinuteWritable(String stopId, String hourMinute) {
        this.stopId = new Text(stopId);
        this.hourMinute = new Text(hourMinute);
    }

    public Text getStopId() {
        return stopId;
    }

    public void setStopId(String stopId) {
        this.stopId.set(stopId);
    }

    public Text getHourMinute() {
        return hourMinute;
    }

    public void setHourMinute(String hourMinute) {
        this.hourMinute.set(hourMinute);
    }

    @Override
    public void write(DataOutput out) throws IOException {
        stopId.write(out);
        hourMinute.write(out);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        stopId.readFields(in);
        hourMinute.readFields(in);
    }

    @Override
    public int compareTo(StopHourMinuteWritable other) {
        int cmp = this.stopId.compareTo(other.stopId);
        if (cmp != 0) {
            return cmp;
        }
        return this.hourMinute.compareTo(other.hourMinute);
    }

    @Override
    public int hashCode() {
        return Objects.hash(stopId, hourMinute);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null || getClass() != obj.getClass())
            return false;
        StopHourMinuteWritable that = (StopHourMinuteWritable) obj;
        return stopId.equals(that.stopId) && hourMinute.equals(that.hourMinute);
    }

    @Override
    public String toString() {
        return stopId.toString() + "\t" + hourMinute.toString();
    }
}